<?php
/**
 * Created by PhpStorm.
 * User: lijie
 * Date: 2018/10/16
 * Time: 14:31
 */
namespace app\api\controller;

use think\Controller;
use app\api\model\Order as OrderModel;
use app\reserve\model\Orderwash;
use think\Request;
use think\Db;

class Diagrammoney extends Controller{

    // 商户订单模型
    private $order;

    public function __construct()
    {
        $this->order = new OrderModel();
    }
    /*
    * Notes: 订单金额统计
    * Create Date: 2018/10/16
    * Create Time: 下午2:44
    * Create by :张福来
    * param :
    */
    public function orderMoney(Request $request)
    {
        $data   = array();
        $arrX    = array();
        $arrY    = array();
        $postData = $request->post();
        if(!empty($postData["a_id"])){
            $wh["a.c_id"] = $postData["a_id"];
        }
            $group = '\'%Y-%m-%d\'';
        if(!empty($postData["yestoday"])) {
            $starttime = $postData["yestoday"] . " 00:00:00";
            $endtime = $postData["yestoday"] . " 23:59:59";
            $wh["a.ctime"] = array(array('EGT', strtotime($starttime)), array('ELT', strtotime($endtime)), 'and');
        } else{
//            $day            = strtotime(date("Y-m-d",time())." 00:00:00");
            $day            = time();
            $wh['a.ctime'] = array("LT",$day);
        }
        // $wh["a.status"] = 4;
        $result = $this->order->orderMoney($wh,$group);
        foreach ($result["data"] as $k=>$v) {
            $arrX[] = $v['group_time'];
            $arrY[] = $v['totalprice'];
        }
        $data['total'] = $result['alltotal']["alltotal"];
        $data['list']['X'] = $arrX;
        $data['list']['Y'] = $arrY;
        if(!empty($result)){
            return apiResponse(200,"成功",$data);
        }else{
            return apiResponse(200,"数据为空",$data);
        }

    }

    /**
     * 洗车商户金额曲线图
     * User: lijie
     * Date: 2018/10/17
     */
    public function orderMoeyWash(Request $request)
    {
        $data   = array();
        $arrX    = array();
        $arrY    = array();
        // $where['a.status'] = 3;
        $param  = $request->post();
        // 商户查看曲线图
        if (!empty($param['a_id'])){
            $where['a.a_id'] = $param['a_id'];
        }
        // 洗车商户查看曲线图
        if (!empty($param['wash_id'])){
            $where['a.wash_id'] = $param['wash_id'];
        }

        // 昨天曲线图
        if(!empty($param["yestoday"])) {
            $starttime = $param["yestoday"] . " 00:00:00";
            $endtime = $param["yestoday"] . " 23:59:59";
            $where["a.ctime"] = array(array('EGT', strtotime($starttime)), array('ELT', strtotime($endtime)), 'and');
        }else{
//            $day            = strtotime(date("Y-m-d",time())." 00:00:00");
            $day            = time();
            $where['a.ctime'] = array("LT",$day);
        }

        // 总条数
        $total = Db::name('orderwash')->alias("a")->where($where)->field("SUM(a.pay_price) total")->find();
        // 格式化时间
        $sql = Db::name('orderwash')->field("id,a_id,wash_id,pay_price,FROM_UNIXTIME(ctime,'%Y-%m-%d') group_time,ctime,status")->buildSql();
        // group分组查询
        $res = Db::table("{$sql}")->alias("a")->where($where)->field("a.group_time,SUM(a.pay_price) totalprice")->group("a.group_time")->select();
        // 整理输出格式
        foreach ($res as $k=>$v) {
            $arrX[] = $v['group_time'];
            $arrY[] = $v['totalprice'];
        }
        $data['total'] = $total['total'];
        $data['list']['X'] = $arrX;
        $data['list']['Y'] = $arrY;
        return apiResponse(200,'获取成功',$data);
    }
    /*
     * Notes: 洗车商户 数据纵览
     * Create Date: 2018/10/20
     * Create Time: 上午9:43
     * Create by :张福来
     * param :
     */
    public function dataScan(Request $request){
        $orderwash = new Orderwash();
        $postData = $request->post();
        $w["wash_id"] = $postData["washid"];
        // $w["status"] = 3;
        if(!empty($param["yestoday"])){
            $starttime = $param["yestoday"]." 00:00:00";
            $endtime = $param["yestoday"]." 23:59:59";
            $wh["ctime"] = array(array('EGT',strtotime($starttime)),array('ELT',strtotime($endtime)),'and');
        }else{
//            $day            = strtotime(date("Y-m-d",time())." 00:00:00");
            $day            = time();
            $wh['ctime'] = array("LT",$day);
        }
        $result = $orderwash->dataScan($w,$wh);
        if(!empty($result)){
            return apiResponse(200,"成功",$result);
        }else{
            return apiResponse(200,"数据为空",$result);
        }
    }
}